{% extends 'base_top.html' %}
{% load static %}
{% load address_handle %}

{% block title %}
    提交订单
{% endblock %}


{% block body %}


	
	<h3 class="common_title">确认收货地址</h3>

	<div class="common_list_con clearfix">

        <dl>
        <dt>寄送到：</dt>
                {% if user.default_addr %}
                    <dd>
                        <label>
                        <input type="radio" name="addr_id" checked="" value="{{ user.default_addr.id }}">
                        &nbsp;{% address_handle user.default_addr %}(默认地址)
                        </label>
                        <a href="{% url 'change_site' user.default_addr.id %}?next_url={{ request.get_full_path|urlencode }}">编辑</a>
                    </dd>
                {% endif %}

                {% for addr in user.addressinfo_set.all %}
                    {% if user.default_addr.id != addr.id %}
                        <dd>
                            <label>
                            <input type="radio" name="addr_id" value="{{ addr.id }}">
                            &nbsp;{% address_handle addr %}
                            </label>
                        <a href="{% url 'change_site' addr.id %}?next_url={{ request.get_full_path|urlencode }}">编辑</a>
                        </dd>
                    {% endif %}
                {% empty %}
                    暂时还没地址！
                {% endfor %}

            </dl>


		<a href="{% url 'site' %}" class="edit_site">编辑收货地址</a>

	</div>
	
	<h3 class="common_title">支付方式</h3>	
	<div class="common_list_con clearfix">
		<div class="pay_style_con clearfix">
			<input type="radio" name="pay_style" checked>
			<label class="cash">货到付款</label>
			<input type="radio" name="pay_style">
			<label class="weixin">微信支付</label>
			<input type="radio" name="pay_style">
			<label class="zhifubao"></label>
			<input type="radio" name="pay_style">
			<label class="bank">银行卡支付</label>
		</div>
	</div>

	<h3 class="common_title">商品列表</h3>
	
	<div class="common_list_con clearfix">
        <ul class="goods_list_th clearfix">
			<li class="col01">商品名称</li>
			<li class="col02">商品单位</li>
			<li class="col03">商品价格</li>
			<li class="col04">数量</li>
			<li class="col05">小计</li>
		</ul>
    {% if carts %}
        {% for cart in carts %}
            <ul class="goods_list_td clearfix" data-cid="{{ cart.id }}">
                <li class="col01">{{ forloop.counter }}</li>
                <li class="col02"><a href="{% url 'detail' cart.goods.id %}"><img src="/static/{{ cart.goods.gpic }}"></a></li>
                <li class="col03"><a href="{% url 'detail' cart.goods.id %}">{{ cart.goods.gtitle }}</a></li>
                <li class="col04">{{ cart.goods.gunit }}</li>
                <li class="col05">{{ cart.goods.gprice }}元</li>
                <li class="col06">{{ cart.count }}</li>
                <li class="col07">0.00元</li>
            </ul>
        {% endfor %}
    {% else %}
        <ul class="goods_list_td clearfix" data-gid="{{ goods.id }}">
            <li class="col01">1</li>
            <li class="col02"><a href="{% url 'detail' goods.id %}"><img src="/static/{{ goods.gpic }}"></a></li>
            <li class="col03"><a href="{% url 'detail' goods.id %}">{{ goods.gtitle }}</a></li>
            <li class="col04">{{ goods.gunit }}</li>
            <li class="col05">{{ goods.gprice }}元</li>
            <li class="col06">{{ count }}</li>
            <li class="col07">0.00元</li>
        </ul>
    {% endif %}


	</div>

	<h3 class="common_title">总金额结算</h3>

	<div class="common_list_con clearfix">
		<div class="settle_con">
			<div class="total_goods_count">共<em>0</em>件商品，总金额<b>0.00元</b></div>
			<div class="transit">运费：<b>{{ fare }}元</b></div>
			<div class="total_pay">实付款：<b>0.00元</b></div>
		</div>
	</div>

	<div class="order_submit clearfix">
		<a href="javascript:;" id="order_btn">提交订单</a>
	</div>	



	<div class="popup_con">
		<div class="popup">
			<p>订单提交成功！</p>
		</div>
		
		<div class="mask"></div>
	</div>



    <script>

        $('#order_btn').click(function() {
			//localStorage.setItem('order_finish',2);

            // 拿到所有的商品id及数量， 如果从购物车过来，也要拿到对应购物车的id
            var $goods_list = $('.goods_list_td');
            var data = {};
            if ($goods_list.length == 1 && $goods_list.eq(0).data('gid')) {
                // 通过立即购买,获取gid和count
                var gid = $goods_list.data('gid');
                data['gid'] = gid;
                data['count'] = parseInt($goods_list.find('.col06').text());
            } else {
                // 通过购物车,获取cid
                // 拿到购物车id数组
                var ids = [];
                $goods_list.each(function () {
                    var id = $(this).data('cid');
                    ids.push(id);
                });
                data['cids'] = ids;
            }

            data['addr_id'] = $('[name="addr_id"]:checked').val();

            var $popup = $('.popup_con');
            $.post({
                url: "{% url 'order_handle' %}",
                data: data,
                traditional:true,
                success: function (data) {
                    console.log(data);
                    if (data.status == 200){
                        $popup.fadeIn('fast', function() {
                            setTimeout(function(){
                                $('.popup_con').fadeOut('fast',function(){
                                    window.location.href = data.url;
                                });
                            },3000)
                        });
                    } else {
                        // alert('失败！');
                        $popup.find('p').text('订单提交失败！');
                        $popup.fadeIn('fast', function() {
                            setTimeout(function(){
                                $popup.fadeOut('fast',function(){
                                    window.location.href = "{% url 'cart' %}"
                                });
                            },3000)
                        });

                    }

                }
            })






		});


        function total() {
            var $goods_list = $('.goods_list_td');
            var total = 0;
            var count = 0;
            $goods_list.each(function () {
                var t = $(this).find('.col05').text();
                var price = parseFloat(t.substring(0, t.length-1));
                var c = parseInt($(this).find('.col06').text());
                var subtotal = price * c;
                $(this).find('.col07').text(subtotal.toFixed(2) + '元');
                count += 1;
                total += subtotal;
            });

            var fare = $('.transit b').text();
            fare = parseFloat(fare.substring(0, fare.length-1));
            var total_pay = total + fare;

            $('.total_goods_count em').text(count);
            $('.total_goods_count b').text(total.toFixed(2) + '元');
            $('.total_pay b').text(total_pay.toFixed(2) + '元');


        }

        $(function () {
            total()
        })



    </script>



{% endblock %}